//
//  OPNoteCollectionReusableView.m
//  OnePiece
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 wsy. All rights reserved.
//

#import "OPNoteCollectionCell.h"

@interface OPNoteCollectionCell () <UIScrollViewDelegate>

@end


@implementation OPNoteCollectionCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _time = [[UILabel alloc]initWithFrame:CGRectMake(20, SCREEN_HEIGHT * 0.9, SCREEN_WIDTH - 40, 15)];
        _time.font = [UIFont systemFontOfSize:14];
        _time.textColor = [UIColor whiteColor];
        _time.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.246];
        [self.contentView addSubview:_time];
    
        _like = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_like setImage:[UIImage imageNamed:@"zyciconfont-hongxin (2)"] forState:(UIControlStateNormal)];
        _like.frame = CGRectMake(20, SCREEN_HEIGHT - 30, 20, 20);
        [self addSubview:_like];
        
        _down = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_down setImage:[UIImage imageNamed:@"zyciconfont-xiazai (3)"] forState:(UIControlStateNormal)];
        _down.frame = CGRectMake(100, SCREEN_HEIGHT - 30, 20, 20);
        [self addSubview:_down];
        
        _share = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_share setImage:[UIImage imageNamed:@"zyciconfont-fenxiang"] forState:(UIControlStateNormal)];
        _share.frame = CGRectMake(180, SCREEN_HEIGHT - 30, 20, 20);
        [self addSubview:_share];
        
        _page = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 100, SCREEN_HEIGHT - 30, 100, 20)];
        _page.textColor = [UIColor whiteColor];
        _page.textAlignment = NSTextAlignmentCenter;
        _page.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
        [self addSubview:_page];
        
    }
    return self;
}


- (void)configureDataWithDic:(NSDictionary *)m
{
    [_coverImg removeFromSuperview];
    [_words removeFromSuperview];
    
    if (((NSString *)m[@"picfile"]).length > 0)
    {
        self.coverImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 2 - SCREEN_WIDTH * [[m valueForKey:@"pich"] doubleValue] / [[m valueForKey:@"picw"] doubleValue] / 2, SCREEN_WIDTH, SCREEN_WIDTH * [[m valueForKey:@"pich"] doubleValue] / [[m valueForKey:@"picw"] doubleValue])];
        [self.coverImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.117go.com/timg/upload/%@",m[@"picfile"]]]];
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT)];
        _scrollView.minimumZoomScale = 1;
        _scrollView.maximumZoomScale = 2;
        _scrollView.delegate = self;
        [_scrollView addSubview:_coverImg];
        [self.contentView addSubview:_scrollView];
    }
    if (((NSString *)m[@"words"]).length > 0)
    {
        self.words = [[UIScrollView alloc]initWithFrame:CGRectMake(0,  SCREEN_HEIGHT * 0.7 , SCREEN_WIDTH, SCREEN_HEIGHT * 0.2 - 10)];
        _words.backgroundColor = [UIColor clearColor];
        UILabel *label = [[UILabel alloc]init];
        if ([self heightForString:m[@"words"]] < SCREEN_HEIGHT * 0.2 - 10) {
            label.frame = CGRectMake(20, SCREEN_HEIGHT * 0.2 - 10 - [self heightForString:m[@"words"]], SCREEN_WIDTH - 40, [self heightForString:m[@"words"]]);
        }
        else
        {
            label.frame = CGRectMake(20, 0, SCREEN_WIDTH - 40, [self heightForString:m[@"words"]]);
        }
        label.text = m[@"words"];
        label.textColor = [UIColor whiteColor];
        label.backgroundColor = [UIColor clearColor];
        label.numberOfLines = 0;
        
        label.font = [UIFont systemFontOfSize:14];
        [_words addSubview:label];
        _words.contentSize = CGSizeMake(SCREEN_WIDTH - 40, [self heightForString:m[@"words"]]);
        [self.contentView addSubview:_words];
    }
    
    if (((NSString *)m[@"words"]).length > SCREEN_HEIGHT * 0.2 - 10 && ((NSString *)m[@"picfile"]).length == 0)
    {
        _words.frame = CGRectMake(0, SCREEN_HEIGHT * 0.9 - [self heightForString:m[@"words"]] - 20, SCREEN_WIDTH, [self heightForString:m[@"words"]]);
    }
    _time.text = [NSString stringWithFormat:@"🕒 %@",m[@"timestamp"]];
    [self.contentView bringSubviewToFront:_time];
    

}


- (CGFloat)heightForString:(NSString *)string
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
    CGRect bound = [string boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return bound.size.height;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return scrollView.subviews[0];
}



@end
